package com.softroad.help.app.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.arnx.jsonic.JSON;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;

import com.softroad.help.app.AppManager;
import com.softroad.help.app.R;
import com.softroad.help.app.adapter.ReportAdapter;
import com.softroad.help.app.bean.ReportBean;
import com.softroad.help.app.bean.ReportResponse;
import com.softroad.help.app.constant.Apis;
import com.softroad.help.app.handler.StringHandler;
import com.softroad.help.app.tools.HttpTool;
import com.softroad.help.app.tools.StringTool;
import com.softroad.help.app.tools.ToastTool;

public class ReportListActivity extends BaseActivity {
	private GridView grideview;
	private ReportAdapter adapter;

	public static void startActivity(Context context) {
		context.startActivity(new Intent(context, ReportListActivity.class));
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.main;
	}

	@Override
	public void initView(View view) {
		action_title.setText(R.string.tab2);
		grideview = $(R.id.grideview);
		adapter = new ReportAdapter(this, new OnItemClick() {

			@Override
			public void onClick(int position, ReportBean bean) {
				ReportActivity.startActivity(getContext(), bean);
			}
		});
		grideview.setAdapter(adapter);
		action_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				AppManager.getAppManager().finishActivity();
			}
		});
	}

	public interface OnItemClick {
		public void onClick(int position, ReportBean bean);
	}

	@Override
	public void initData(Context mContext) {
		getReportList();
	}

	@Override
	public void initListener() {

	}

	@Override
	public void resume() {

	}

	@Override
	public void destroy() {

	}

	private void getReportList() {

		Map maps = new HashMap<String, String>();

		HttpTool.get(getContext(), Apis.HOST + Apis.LOAD, maps,
				new StringHandler(loading) {

					@Override
					public void success(String response) {
						if (StringTool.isNoBlankAndNoNull(response)) {
							ReportResponse bean = JSON.decode(response,
									ReportResponse.class);
							if (bean != null) {
								String status = bean.getStatus();
								if (StringTool.isNoBlankAndNoNull(status)
										&& "0".equalsIgnoreCase(status)) {
									List<ReportBean> list = bean.getData();
									if (list != null) {
										adapter.setData(list);
										adapter.notifyDataSetChanged();
									}
								} else {
									ToastTool.toastMessage(getContext(),
											bean.getMessage());
								}
							}
							loading.setVisibility(View.GONE);
						}
					}

					@Override
					public void failure(int statusCode, String responseBody,
							Throwable e) {

					}
				}, "utf-8");
	}
}
